package extra01GUI;

import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class VTriangulo extends JDialog implements ActionListener {

	private JComboBox cbLista;
	
	private JLabel lblLadoA;
	private JLabel lblLadoB;
	private JLabel lblLadoC;
	
	private JTextField txtLadoA;
	private JTextField txtLadoB;
	private JTextField txtLadoC;
	
	private JButton btnCalcular;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public VTriangulo(JFrame F){
		super(F,"Triangulo");
		
		cbLista = new JComboBox(new String[]{"Equilatero","Isoceles","Escaleno"});
		cbLista.addActionListener(this);
		
		btnCalcular = new JButton("Calcular");
		btnCalcular.addActionListener(this);
		
		lblLadoA = new JLabel("Lado A:");		
		txtLadoA = new JTextField(20);
		
		lblLadoB = new JLabel("Lado B:");
		lblLadoB.setVisible(false);
		txtLadoB = new JTextField(20);
		txtLadoB.setVisible(false);
		
		lblLadoC = new JLabel("Lado C:");
		lblLadoC.setVisible(false);		
		txtLadoC = new JTextField(20);
		txtLadoC.setVisible(false);
		
		FlowLayout fl = new FlowLayout(FlowLayout.RIGHT);
		JPanel pa = new JPanel(fl);
		JPanel pb = new JPanel(fl);
		JPanel pc = new JPanel(fl);
		JPanel pd = new JPanel(fl);	
		
		pa.add(lblLadoA);
		pa.add(txtLadoA);
		
		pb.add(lblLadoB);
		pb.add(txtLadoB);
		
		pc.add(lblLadoC);
		pc.add(txtLadoC);
		
		pd.add(cbLista);
		pd.add(btnCalcular);
		
		setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
		add(pd);
		add(pa);
		add(pb);
		add(pc);
		
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		Point punto = F.getLocation();
		setLocation(punto.x, punto.y);
	}
	
	public void Iniciar(){
		pack();
		setResizable(false);
		setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == cbLista){
			switch (cbLista.getSelectedIndex()) {
			case 0:
				lblLadoB.setVisible(false);
				txtLadoB.setVisible(false);
				
				lblLadoC.setVisible(false);
				txtLadoC.setVisible(false);
				break;
			case 1:
				lblLadoB.setVisible(true);
				txtLadoB.setVisible(true);
				
				lblLadoC.setVisible(false);
				txtLadoC.setVisible(false);
				break;				
			case 2:
				lblLadoB.setVisible(true);
				txtLadoB.setVisible(true);
				
				lblLadoC.setVisible(true);
				txtLadoC.setVisible(true);
				break;
			default:
				break;
			}
			pack();
		}
		
		if(e.getSource() == btnCalcular){
			MostrarInfo(cbLista.getSelectedIndex());
		}

	}

	private void MostrarInfo(int opc) {
		switch (opc) {
		case 0:
			InfoEquilatero();
			break;
		case 1:
			InfoIsoceles();
			break;
		case 2:
			InfoEscaleno();
			break;
		default:
			break;
		}
	}
	
	private void InfoEquilatero(){
		double a;
		boolean estado = true;
		
		try{
			a = Double.parseDouble(txtLadoA.getText());
		}catch(NumberFormatException e){
			a = 0;
			estado=false;
		}
		
		if(estado){
			JOptionPane.showMessageDialog(this, UtilidadGeometria.Triangulo(new double[] {a}), "Resultado", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(this,"Error en el ingreso de los datos", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void InfoIsoceles(){
		double a,b;
		boolean estado = true;
		
		try{
			a = Double.parseDouble(txtLadoA.getText());
		}catch(NumberFormatException e){
			a = 0;
			estado=false;
		}
		
		try{
			b = Double.parseDouble(txtLadoB.getText());
		}catch(NumberFormatException e){
			b = 0;
			estado=false;
		}
		
		if(estado){
			JOptionPane.showMessageDialog(this, UtilidadGeometria.Triangulo(new double[] {a,b}), "Resultado", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(this,"Error en el ingreso de los datos", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void InfoEscaleno(){
		double a,b,c;
		boolean estado = true;
		
		try{
			a = Double.parseDouble(txtLadoA.getText());
		}catch(NumberFormatException e){
			a = 0;
			estado=false;
		}
		
		try{
			b = Double.parseDouble(txtLadoB.getText());
		}catch(NumberFormatException e){
			b = 0;
			estado=false;
		}
		
		try{
			c = Double.parseDouble(txtLadoC.getText());
		}catch(NumberFormatException e){
			c = 0;
			estado=false;
		}
		
		if(estado){
			JOptionPane.showMessageDialog(this, UtilidadGeometria.Triangulo(new double[] {a,b,c}), "Resultado", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(this,"Error en el ingreso de los datos", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}
